import to_page from '../../../commons/pageaddress.js'
import util from '../../../utils/util.js'
import api from '../../../requests/api.js'
let app = getApp()
var params

Page({
  data: {
    prompt: {
      hidden: !0,
      icon: '../../../assets/coupons/default-coupons.png',
      title: '暂无订单',
    },
    selectedTab: 0, // 当前的tab页面下标
    tabs: [],
    currentPage: 1,
    pageSize: 10,
    loading: true, //加载框是否显示
    searchLoading: true, //把"上拉加载"的变量设为true，显示  
    searchLoadingComplete: false //把“没有数据”设为false，隐藏  

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    new app.WeToast();
    this.setData({
      selectedTab: options.index,
    })
    this.data.currentPage = 1;
    this.request_page(parseInt(options.index));
    wx.getSystemInfo({
      success: (res) => {
        let height = util.px2rpx(res.windowHeight * res.pixelRatio) - 91
        this.setData({
          swiperHeight: height
        })
      },
    })
  },

  /**
   * 当页面滑动到底部时触发的方法,删掉这个方法后控制台会报事件警告
   */
  onReachBottom: function() {},

  // 点击每一行 跳转订单详情
  selectedCell: function(e) {

    var code = e.currentTarget.dataset.code;

    var url = to_page.t + to_page.goOrderDetail + '?orderCode=' + code + '&isList=true';
    to_page.to(url)

  },

  // 点击 “立即支付” or “确认收货” 按钮
  selectedTypeCell: function(e) {
    const t = this;
    var $mold = e.currentTarget.dataset.mold;
    var code = e.currentTarget.dataset.code;
    if ($mold == 'pay') {

      var url = to_page.t + to_page.goOrderDetail + '?orderCode=' + code;
      to_page.to(url)


    } else if ($mold == 'sure') {
      wx.showModal({
        title: '提示',
        content: '确定收货吗？',
        success: function(res) {
          if (res.confirm) { // 点击确定
            t.request_put_order(code)
          }
        }
      })
    }
  },

  // 刷新当前页面的列表数据 从第一页开始
  refresh_current_data: function() {
    const that = this;
    var ntabs = this.data.tabs;
    ntabs = [];
    this.setData({
      tabs: ntabs
    })
    params["currentPage"] = 1;
    that.my_orderList(params)

  },

  //请求网络 -- 用户确认收货
  request_put_order: function(orderCode) {
    const that = this;
    wx.showLoading({
      title: '',
    })
    api.put_order(orderCode, (res) => {
      that.refresh_current_data()
    }, (data) => {
      if (util.alertNetworkError(data)) {
        that.wetoast.toast({
          title: data.resMsg,
        })
      }
    }, () => {});
  },
  // 请求当前页面page的数据
  request_page: function(page) {
    var ntabs = this.data.tabs;
    ntabs = []
    this.data.selectedTab = page;
    this.setData({
      selectedTab: page,
      tabs: ntabs
    })
    switch (this.data.selectedTab) {
      case 0:
        params = {
          currentPage: this.data.currentPage,
          pageSize: this.data.pageSize,
        }
        break
      case 1:
        params = {
          orderStatusList: [0],
          currentPage: this.data.currentPage,
          pageSize: this.data.pageSize,
        }
        break
      case 2:
        params = {
          orderStatusList: [1, 2],
          currentPage: this.data.currentPage,
          pageSize: this.data.pageSize,
        }
        break
      case 3:
        params = {
          orderStatusList: [3],
          currentPage: this.data.currentPage,
          pageSize: this.data.pageSize,
        }
        break
    }
    this.my_orderList(params)
  },
  // 顶部tab 点击事件
  changeTab: function(event) {
    var index = parseInt(event.currentTarget.dataset.tabid);
    this.data.currentPage = 1
    this.request_page(index);
  },

  //NET

  //我的订单列表
  my_orderList: function(params, ) {
    const self = this;
    api.getOrders(params, (res) => {
      if (res.data.orderLists.length == 0) { //没有数据
        self.setData({
          searchLoadingComplete: true, //把“没有数据”设为true，显示

        })
      } else { //说明还有数据
        self.setData({
          searchLoadingComplete: false, //把“没有数据”设为false，隐藏  

        })
      };
      var ntabs = this.data.tabs;
      ntabs = ntabs.concat(res.data.orderLists)
      self.setData({
        tabs: ntabs, //给数组赋值，
        loading: true, //隐藏加载框
        'prompt.hidden': ntabs.length,

      })

    }, (data) => {
      self.setData({
        searchLoadingComplete: true, //把“没有数据”设为true，显示
        loading: true //隐藏加载框
      })
      if (util.alertNetworkError(data)) {
        self.wetoast.toast({
          title: data.resMsg,
        })
      }

    }, () => {
      self.setData({
        searchLoading: false //把"上拉加载"的变量设为false，隐藏  

      })
    });
  },
  //上拉触底
  moreData: function() {
    let that = this;
    //如果还有数据就继续加载
    if (!that.data.searchLoading && !that.data.searchLoadingComplete) {
      this.data.currentPage = this.data.currentPage + 1
      that.setData({
        searchLoading: true //把"上拉加载"的变量设为false，隐藏

      })
      params["currentPage"] = this.data.currentPage;
      this.my_orderList(params)
    }
  },
  onReachBottom: function(event) {
    this.moreData()
  },

})